#!/bin/bash
##
# Date: 2018-03-05
# Author: phiix
# Describe: yum install mysql
##
trap "{echo Interrupt by keyboard Ctrl-C;exit 1 ; }" SIGINT

export PATH=/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export LANG=en_US.UTF-8

current_dir=`pwd`

config_path="../configs/mysql"
install_log="/root/mysql_install.log"


if [ ! -f /etc/yum.repos.d/mysql-community.repo ];then
    cp ${config_path}/mysql-community.repo /etc/yum.repos.d/
fi

echo "Select mysql version:
1) mysql-5.5
2) mysql-5.6 (default)
3) mysql-5.7
"
read -t 10 -p "Enter your choice: " choice
case ${choice:-2} in 
	1)
	mysql_version="mysql-5.5"
	;;
	2)
	mysql_version="mysql-5.6"
	;;
	3)
	mysql_version="mysql-5.7"
	;;
	*)
	read -t 10 -p "Error choice,will install mysql-5.6 [y/n] (default: n)? " reply
    if [ "${reply:-n}" == "y" ];then
		mysql_version="mysql-5.6"
	else
		echo "Exit..."
		exit 1
	fi  
	;;
esac

mysql_pkgs="mysql-community-server mysql-community-libs mysql-community-devel mysql-community-common mysql-community-client"

install_mysql_5.5 () {
sed -i "/mysql-5.7/{n;s/^enabled.*/enabled=0/}" /etc/yum.repos.d/mysql-community.repo
sed -i "/mysql-5.6/{n;s/^enabled.*/enabled=0/}" /etc/yum.repos.d/mysql-community.repo
sed -i "/mysql-5.5/{n;s/^enabled.*/enabled=1/}" /etc/yum.repos.d/mysql-community.repo
yum install -y ${mysql_pkgs}
}

install_mysql_5.6 () {
sed -i "/mysql-5.7/{n;s/^enabled.*/enabled=0/}" /etc/yum.repos.d/mysql-community.repo
sed -i "/mysql-5.6/{n;s/^enabled.*/enabled=1/}" /etc/yum.repos.d/mysql-community.repo
sed -i "/mysql-5.5/{n;s/^enabled.*/enabled=0/}" /etc/yum.repos.d/mysql-community.repo
yum install -y ${mysql_pkgs}
}

install_mysql_5.7 () {
sed -i "/mysql-5.7/{n;s/^enabled.*/enabled=1/}" /etc/yum.repos.d/mysql-community.repo
sed -i "/mysql-5.6/{n;s/^enabled.*/enabled=0/}" /etc/yum.repos.d/mysql-community.repo
sed -i "/mysql-5.5/{n;s/^enabled.*/enabled=0/}" /etc/yum.repos.d/mysql-community.repo
yum install -y ${mysql_pkgs}
}

echo -e "\n--->Start install ${mysql_version}<---" |tee ${install_log}

case ${mysql_version} in
	mysql-5.5)
	install_mysql_5.5 |tee -a ${install_log}
	;;
	mysql-5.6)
	install_mysql_5.6 |tee -a ${install_log}
	;;
	mysql-5.7)
	install_mysql_5.7 |tee -a ${install_log}
	;;
	*)
	echo "Error.View install log, and solve the problem, then re executing this script." |tee -a ${install_log}
	exit 1
esac

exit 0

